package com.qpx.shoppingcarservice.web;


import com.qpx.common.dto.CarDto;
import com.qpx.common.dto.ItemDto;
import com.qpx.common.dto.ShoppingCarDto;
import com.qpx.common.dto.UserDto;
import com.qpx.common.vo.ResultEntity;
import com.qpx.shoppingcarservice.entity.ShoppingCarEntity;
import com.qpx.shoppingcarservice.service.GoodsService;
import com.qpx.shoppingcarservice.service.ShoppingCarService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ShoppingCarController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    ShoppingCarService shoppingCarService;

    @Autowired
    MongoTemplate mongoTemplate;

    @RequestMapping("/car/add")
    public ResultEntity addCar(CarDto carDto, HttpServletRequest request){
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("user");
        if (obj==null){
            return ResultEntity.error("401","尚未登录",session.getId());
        }
        //根据  商品id 查询 商品信息
        ItemDto itemDto = goodsService.getItemById(carDto.getItemId());

        ShoppingCarEntity shoppingCarEntity = new ShoppingCarEntity();
        BeanUtils.copyProperties(itemDto,shoppingCarEntity);
        shoppingCarEntity.setItemId(itemDto.getId());
        shoppingCarEntity.setSum(carDto.getSum());
        UserDto userDto = (UserDto) obj;
        shoppingCarEntity.setUid(userDto.getId());
        shoppingCarService.addCar(shoppingCarEntity);
        return ResultEntity.ok(session.getId());
    }

    @RequestMapping("/car/update")
    public ResultEntity updateCar(CarDto carDto, HttpServletRequest request){
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("user");
        if (obj==null){
            return ResultEntity.error("401","尚未登录",session.getId());
        }
        ShoppingCarEntity shoppingCarEntity = new ShoppingCarEntity();
        shoppingCarEntity.setItemId(carDto.getItemId());
        shoppingCarEntity.setSum(carDto.getSum());
        UserDto userDto = (UserDto) obj;
        shoppingCarEntity.setUid(userDto.getId());
        shoppingCarService.updateCar(shoppingCarEntity);
        return ResultEntity.ok(session.getId());
    }


    @RequestMapping("/myCarList")
    public ResultEntity myCarList(ShoppingCarDto shoppingCarDto,HttpSession session){
        Query query = new Query();
        Object object = session.getAttribute("user");
        if (object==null){
            return ResultEntity.error("401","尚未登录",session.getId());
        }
        UserDto userDto = (UserDto)object;
        query.addCriteria(Criteria.where("uid").is(userDto.getId()));
        if (StringUtils.isNotBlank(shoppingCarDto.getItemBrand())){
            query.addCriteria(Criteria.where("itemBrand").is(shoppingCarDto.getItemBrand()));
        }
        if (StringUtils.isNotBlank(shoppingCarDto.getItemName())){
            query.addCriteria(Criteria.where("itemName").is(shoppingCarDto.getItemName()));
        }

        long count = mongoTemplate.count(query, ShoppingCarEntity.class);
        query.skip((shoppingCarDto.getPageNo()-1)*shoppingCarDto.getPageSize());
        query.limit(shoppingCarDto.getPageSize());
        List<ShoppingCarEntity> shoppingCarEntities = mongoTemplate.find(query, ShoppingCarEntity.class);
        Map<String,Object> map = new HashMap<String,Object>(){{
            put("cout",count);
            put("list",shoppingCarEntities);
        }};
        return ResultEntity.ok(map);
    }
}
